const userModel = require("../db/mysql.js");
const moment = require("moment");
/**
 * 重置到文章页
 */
/* exports.getRedirectPosts = async ctx => {
  ctx.redirect("/posts");
}; */

/**
 * 文章页
 */
console.log("enter c-post");

exports.getPosts = async ctx => {
  console.log("getpost");
  await userModel.findAllPost()
  .then(result => {
    ctx.body = {
      code: 200,
      message: "获取成功",
      data:result,
    };
    ctx.body = result
  }).catch(() => {
    ctx.body = {
      code: 500,
      message: "发表文章失败"
    };
  })
};

/**
 * 发表文章
 */
exports.postCreate = async ctx => {
  console.log('postCreate')
  let { title, content } = ctx.request.body,
    time = moment().format("YYYY-MM-DD HH:mm:ss");

  await userModel
    .insertPost([title, content, time])
    .then(() => {
      ctx.body = {
        code: 200,
        message: "发表成功"
      };
    })
    .catch(() => {
      ctx.body = {
        code: 500,
        message: "发表文章失败"
      };
    });
};
